//
//  ByBlueToothMeaturesModel.m
//  laserdevice_ios
//
//  Created by pro on 16/12/10.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByBlueToothMeaturesModel.h"

@implementation ByBlueToothMeaturesModel

- (instancetype)initWithDataHexString:(NSString *)hexStr {
    
    if (self = [super init]) {
        
        self.unit = [ByCommonCache getSystemUnit].intValue;
        
        self.name = @"测量模型";

        // 数据包个数
        NSString *numOfDatasStr = [hexStr substringWithRange:NSMakeRange(34, 2)];
        self.numOfData = [[self getNumberFromHexStr:numOfDatasStr] integerValue];

    }
    return self;
}

- (NSString *)getNumberFromHexStr:(NSString *)hexStr {
    
    NSMutableString *retStr = [[NSMutableString alloc] init];
    
    for (int i = 0; i < hexStr.length; i += 2) {
        NSString *sub = [hexStr substringWithRange:NSMakeRange(i, 2)];
        char c1 = [sub characterAtIndex:0];
        char c2 = [sub characterAtIndex:1];
        NSString *subData = [NSString stringWithFormat:@"%c%c", c1, c2];
        
        // 16进制字符串转数字
        unsigned long long result = 0;
        NSScanner *scanner = [NSScanner scannerWithString:subData];
        [scanner scanHexLongLong:&result];
        
        // 拼接字符串
        [retStr appendFormat:@"%c", (char)result];
    }
    
    return retStr;
}

- (NSString *)getStringFromLength:(CGFloat)length {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    switch (unit) {
        case 0: // 米
            return [NSString stringWithFormat:@"%.3f", length];
            break;
        case 1: // 英尺
//            return [NSString stringWithFormat:@"%.3f", length * 3.2808399];
            return [NSString stringWithFormat:@"%.3f", length / 0.0254 / 12];

            break;
        case 2: // 英寸
//            return [NSString stringWithFormat:@"%.2f", length * 3.2808399 * 12];
            return [NSString stringWithFormat:@"%.3f", length / 0.0254];

            break;
        case 3: // 英尺+英寸
            return [NSString stringWithFormat:@"%.0f\'%.2f\"", floorf(length * 3.2808399), ((length * 3.2808399) - floorf(length * 3.2808399)) * 12.000];
            break;
        default:
            break;
    }
    
    
    return nil;
}

- (NSDictionary *)saveDic {
    
    return nil;
}

- (instancetype)initWithJsonDic:(NSDictionary *)jsonDic {
    
    if (self = [super init]) {
        
        self.name = jsonDic[@"name"];
    }
    return self;
}

@end

